package com.prolestar.web.controller.system;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prolestar.common.annotation.Anonymous;
import com.prolestar.common.core.controller.BaseController;
import com.prolestar.common.core.domain.AjaxResult;
import com.prolestar.common.core.domain.entity.SysUser;
import com.prolestar.common.exception.ServiceException;
import com.prolestar.common.utils.SecurityUtils;
import com.zhihuixingguang.login.CreateLoginUrlRes;
import com.zhihuixingguang.login.LoginService;
import com.zhihuixingguang.login.TicketCallBackReq;
import com.zhihuixingguang.login.TicketCallBackRes;
import com.zhihuixingguang.login.TicketRes;
import com.zhihuixingguang.login.ZhxgResBean;
import com.zhihuixingguang.util.JSONUtil;
import com.zhihuixingguang.util.StringUtils;

import io.swagger.annotations.Api;

@RestController
@RequestMapping(value = "/zhihuixingguang")
@Api("智慧星光对接")
public class ZhihuixingguangController extends BaseController {

	Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private LoginService loginService;

	private Map<String, String> ticketCache = new ConcurrentHashMap<String, String>();
	@Anonymous
	@GetMapping(value = "/ticket")
	public ZhxgResBean<TicketRes> ticket(String ticket,HttpServletRequest request,HttpServletResponse response){
		String sign = request.getHeader("sign");
		String time = request.getHeader("time");
		String nonce = request.getHeader("nonce");
		String appid = request.getHeader("appid");
		String openid = ticketCache.get(ticket);
		TicketCallBackRes r = loginService.ticketCheck(new TicketCallBackReq(appid, nonce,Long.valueOf(time),ticket,sign,openid));
		response.setHeader("sign", r.getSign());
		response.setHeader("nonce", r.getNonce());
		response.setHeader("appid", r.getAppid());
		response.setHeader("time", r.getTime()+"");
		ZhxgResBean<TicketRes> res =  new ZhxgResBean<TicketRes>(new TicketRes(r.getSystemid(),r.getOpenid()));
		res.setCode(200);
		log.info("resultBean:{}",JSONUtil.toJSONString(res, true));
		return res;
	}


	/**
	 *
	 * @return
	 */
	
	@PostMapping(value = "/createTicketUrl")
	public AjaxResult createTicketUrl(){
		SysUser user = SecurityUtils.getLoginUser().getUser();
		//这里设置获取当前人员的unionId数据
		String unionId = user.getThirdToken();
		if(!user.isTokenStatus()) {
			throw new ServiceException("当前人员未开启登录到智慧星光");			
		}
		log.info("获取到的unionId:{}",unionId);
		String ticket = StringUtils.randomStr(32);
		ticketCache.put(ticket, unionId);
		CreateLoginUrlRes r = loginService.createLoginUrl(ticket);
		return success(r);
	}
}
